package com.cfs.utils;

import java.io.Serializable;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Administrator on 2017/9/28/028.
 */
public class JedisUtils {

    private static JedisPool jedisPool8 = null;
    private static JedisPool jedisPool9 = null;

    private static String host = "127.0.0.1";
    private static String password = "Xjk34LkKJdf34";
    private static int port = 6379;
    private static int DB_INDEX_8 = 8;
    private static int DB_INDEX_9 = 9;
    private static int TIMEOUT = 3000;


    public static void set(Serializable key, Object val, int second) {
    	getJedis8().setex(SerializeUtils.serialize(key), second, SerializeUtils.serialize(val));
    }

    public static Object get(Serializable key) {
        return SerializeUtils.deserialize(getJedis8().get(SerializeUtils.serialize(key)));
    }

    public static void remove(Serializable key) {
    	getJedis8().del(SerializeUtils.serialize(key));
    }

    public static Long getDBSize(){
        return getJedis8().dbSize();
    }

    public static void dbFlush(){
    	getJedis8().flushDB();
    }



    private static Jedis getJedis8() {
        if (jedisPool8 != null) {
            return jedisPool8.getResource();
        } else {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxWaitMillis(3000L);
            jedisPoolConfig.setMaxTotal(128);
            jedisPoolConfig.setMaxIdle(56);
            jedisPoolConfig.setTestOnBorrow(true);
            jedisPoolConfig.setMinIdle(28);
            jedisPool8 = new JedisPool(jedisPoolConfig, host, port, TIMEOUT, password, DB_INDEX_8);
            return jedisPool8.getResource();
        }
    }
    
    public static Jedis getJedis9() {
        if (jedisPool9 != null) {
            return jedisPool9.getResource();
        } else {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxWaitMillis(3000L);
            jedisPoolConfig.setMaxTotal(128);
            jedisPoolConfig.setMaxIdle(56);
            jedisPoolConfig.setTestOnBorrow(true);
            jedisPoolConfig.setMinIdle(28);
            jedisPool9 = new JedisPool(jedisPoolConfig, host, port, TIMEOUT, password, DB_INDEX_9);
            return jedisPool9.getResource();
        }
    }

}
